package com.szpx.controller.hhxcontroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.hhxentity.Account;
import com.szpx.service.hhxservice.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;
//    @GetMapping
//    public Result<List<Account>>findAll(){
//        List<Account> list=accountService.findAll();
//        return new Result<List<Account>>(true, StatusCode.OK,"查询成功",list);
//    }
//    @GetMapping("/{id}")
//    public Result<Account> findById(@PathVariable Integer id){
//        //调用CategoryService实现根据主键查询Category
//        Account account = accountService.findById(id);
//        return new Result<Account>(true,StatusCode.OK,"查询成功",account);
//    }
//    @PostMapping
//    public Result add(@RequestBody Account account){
//        System.out.println(account);
//        //调用CategoryService实现添加Category
//        accountService.add(account);
//        return new Result(true,StatusCode.OK,"添加成功");
//    }
//    @PutMapping(value = "/{id}")
//    public Result updateBrand(@RequestBody Account account, @PathVariable(value = "id")Integer id) {
//        account.setId(Long.valueOf(id));
//        accountService.update(account);
//        return new Result(true, StatusCode.OK, "修改数据成功");
//    }
//    @PostMapping("/delete")
//    public Result deleteBrand(@RequestBody Account account){
//        accountService.delete(account.hashCode());
//        return new Result(true,StatusCode.OK,"删除数据成功");
//    }

        /*
         * TODO 此方法的用途:
         *
         * 登录功能
         *
         *
         * @return
         * @author
         * @since 2023/3/22 0:09
         */




        /*
         * TODO 此方法的用途:
         *
         * 查看用户详情
         *
         *
         * @return
         * @author
         * @since 2023/3/22 0:09
         */

        @GetMapping("/selectById/{id}")
        public String selectById(@PathVariable("id") Integer id, Model model) {
            Account account = accountService.findById(id);
            if (account != null) {
                model.addAttribute("account",account);
                return "lishihuiTeacherHtml/hhxHtml/delete";
            } else {
                return null;
            }
        }

        /*
         * TODO 此方法的用途:
         *
         * 删去用户
         *
         *
         * @return
         * @author
         * @since 2023/3/22 0:09
         */

        @RequestMapping("/delUser/{id}")
        public String delUser(@PathVariable("id") Integer id) {
            int num = accountService.delete(id);
            if (num > 0)
                return "redirect:/account/ulist"; //重定向到用户列表请求
            else
                return null;
        }


        /*
         * TODO 此方法的用途:
         *
         * 去用户修改页面
         *
         *
         * @return
         * @author
         * @since 2023/3/22 0:09
         */

        @GetMapping("/toUpdateUser/{id}")
        public String toUpdatePage(@PathVariable("id") Integer id, Model model) {
            Account account = accountService.findById(id);
            if (account != null) {
                model.addAttribute("account", account); //将查询出来的信息 传到修改页面
                return "lishihuiTeacherHtml/hhxHtml/update"; //逻辑视图
            } else {
                return null;
            }
        }



        @RequestMapping("/updateUser")
        public String updateUser(Account account) {
            int num = accountService.update(account);
            if (num > 0) {
                return "redirect:/account/ulist";

            } else {
                return null;
            }
        }
        /*
         * TODO 此方法的用途:
         *
         * 增加用户
         *
         *
         * @return
         * @author
         * @since 2023/3/22 0:10
         */

        @RequestMapping("/addUser")
        public String addUser(Account account) {
            accountService.add(account);
                return "redirect:/account/ulist";
        }

        /*
         * TODO 此方法的用途:
         *
         * 去往添加用户页面
         *
         *
         * @return
         * @author
         * @since 2023/3/23 17:28
         */

        @RequestMapping("/toAddUser")
        public String toAddUser(HttpSession session) {
            return "lishihuiTeacherHtml/hhxHtml/add";
        }

        /*
         * TODO 此方法的用途:
         *

        /*
         * TODO 此方法的用途:
         *
         * 查询所有用户
         *
         *
         * @return
         * @author
         * @since 2023/3/22 0:10
         */

        @GetMapping("/ulist")
        public String getList(
                @RequestParam(defaultValue = "1") int pno,
                @RequestParam(defaultValue = "3") int pageSize,
                Account account, Model model) {
            PageHelper.startPage(pno, pageSize); //开始分页
            List<Account> ulist = accountService.findList(account);
            PageInfo<Account> page = new PageInfo<Account>(ulist);
            model.addAttribute("ulist", ulist); //将查询出来的数据放入model对象中
            model.addAttribute("page", page);
            model.addAttribute("id", account.getId());
            model.addAttribute("blance", account);
            model.addAttribute("pno", pno);
            return "lishihuiTeacherHtml/hhxHtml/account1";
        }

//        @RequestMapping("/toHhx")
//        public String toAccount() {
//            return "lishihuiTeacherHtml/hhxHtml/account";
//        }







}
